Thread: [Error] incompatible types in assignment of 'const char [6]' to 'char [80]'

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    [Error] incompatible types in assignment of 'const char [6]' to 'char [80]'

    I can't initialize the string, why is that? It gives the error in the title, but why does it give error? I give it more than enough space to store the string why is it whining?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
    	int phoneNumber;
    	char name[80];
    }People;
    
    int main()
    {
    	
    	People TeC[2];
    	TeC[0].name = "David";
    	
    }

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by telmo_d View Post
    I can't initialize the string, why is that? It gives the error in the title, but why does it give error? I give it more than enough space to store the string why is it whining?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct
    {
        int phoneNumber;
        char name[80];
    }People;
    
    int main()
    {
        
        People TeC[2];
        TeC[0].name = "David";
        
    }
    The last line is an assignment, not an initialization. You should use either strcpy() or strncpy() in order to copy "David" into Tec[0].name

    Or you could initialize the array during definition:
    Code:
    ...
    People Tec[2] = {
       { 12345, "David"},
       { 67890, "Mark"}
    };
    ...
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    So i can't make initializaions, only assignements? Because this line doesn't work either
    Code:
    TeC[0].name = {"David"};
    Now it says "assigning to an array from an initializer list"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by telmo_d
    So i can't make initializaions, only assignements?
    On the contrary, you can initialise arrays (i.e., give them an initial value at the point of definition), but you cannot assign to them (i.e., give them a new value after they have been defined). Adding braces makes no difference to this. What you can do is to assign to their elements (if these are not also arrays), hence the suggestion of strcpy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Ok so i guess that's why people use string functions like strcpy so much. I tried strncpy but what's the advantage of using that one vs strcpy? in strcpy you don't need to say size and it doesn't print the \0

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by telmo_d
    I tried strncpy but what's the advantage of using that one vs strcpy? in strcpy you don't need to say size and it doesn't print the \0
    The size parameter is useful to avoid buffer overflow, though strncpy was not designed specifically to be a size limited version of strcpy since it does not always null terminate and then writes "additional" null characters if the source string is smaller in size than the size argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-19-2014, 04:56 AM
  2. Replies: 9
    Last Post: 04-02-2014, 05:29 PM
  3. Replies: 2
    Last Post: 11-23-2011, 12:30 PM
  4. Replies: 2
    Last Post: 12-26-2009, 10:07 AM
  5. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM